package com.wnhz.wnmap.order.aop;

import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Aspect
@Component
@Slf4j
public class SeataAop {

    @AfterThrowing(throwing = "e",pointcut = "execution(* com.wnhz.wnmap.order.service.*.*(..))")
    public void cutOrder(Throwable e) throws TransactionException {
      log.debug("我进入切面.......");

        String xid = RootContext.getXID();
        //判断xid是否是空
        if(StringUtils.hasText(xid)){
            log.debug("存在分布式全局xid: {}", xid);
            GlobalTransactionContext.reload(xid).rollback();
            throw new TransactionException("座位一售空，请明天再来...");
        }

    }
}
